<?php

namespace Admin\Controller;

class ActivityVoteController extends AdminController
{
    public function index($category_id = null, $pid = null){
        // 获取分类信息
        $category = $this['Category']->info(['id'=>$category_id]);
        $this->assign('category',$category);

        if ( !$category ){
            $this->error('无分类ID或分类不存在');
        }

        if ( !$category['model_id'] ) {
            echo '无模型name';
            exit;
        }

        // 获取该分类的模型信息
        $model = $this['Model']->info(['id'=>$category['model_id']]);


        // 获取文档列表
        $map = [
            'category_id' => $category['id'],
        ];
        if ($pid) {
            $map['pid'] = $pid;
        }
        $list = $this[$model['name']]->get($map);


        // 处理时间与进度
        foreach($list as &$v) {
            if ((time() - $v['start']) >= 0 && (time() - $v['end']) <= 0) {
                // 开始中
                $days = floor(($v['end'] - time())/(24*3600));
                $hours = floor(($v['end'] - time())%(24*3600)/3600);
                $minutes = floor(($v['end'] - time())%3600/60);
                $seconds = floor(($v['end'] - time())%60);
                $str = '距离结束 ';
                if ($days > 0) {
                    $str .= $days . '天';
                }
                if ($hours > 0) {
                    $str .= $hours . '小时';
                }
                if ($minutes > 0) {
                    $str .= $minutes . '分钟';
                }
                $str .= $seconds . '秒';

                $v['time'] = $str;
                $v['progress'] = round((time() - $v['start'])/($v['end'] - $v['start'])*100,2);
                if (100 == $v['progress']) {
                    $v['progress'] = 99.99;
                }

            } else if ((time() - $v['start']) < 0) {
                // 开始前
                $days = floor(($v['start'] - time())/(24*3600));
                $hours = floor(($v['start'] - time())%(24*3600)/3600);
                $minutes = floor(($v['start'] - time())%3600/60);
                $seconds = floor(($v['end'] - time())%60);

                $str = '距离开始 ';

                if ($days > 0) {
                    $str .= $days . '天';
                }
                if ($hours > 0) {
                    $str .= $hours . '小时';
                }
                if ($minutes > 0) {
                    $str .= $minutes . '分钟';
                }

                $str .= $seconds . '秒';

                $v['time'] = $str;
                $v['progress'] = 0;

            } else if ((time() - $v['end']) > 0) {
                // 结束后
                $v['time'] = '已结束';
                $v['progress'] = 100;
                $v['background-color'] = '#d6d6d6';
                $v['animation'] = 'none';
            }
        }

        $this->assign('list',$list);

        // 获取文档模板
        if ($model['template_list']) {
            $template = $model['template_list'];
        } else {
            $template = 'Article/index';
        }
        $this->assign('page', $this[$model['name']]->getPage);
        $this->display($template);
    }
}
